package com.smart.campus.quartz.task;

import com.github.pagehelper.PageHelper;
import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.common.utils.StringUtils;
import com.smart.campus.framework.web.service.DictService;
import com.smart.campus.school.domain.Student;
import com.smart.campus.school.domain.StudentPaper;
import com.smart.campus.school.domain.StudentPaperRelation;
import com.smart.campus.school.domain.WrongQuestion;
import com.smart.campus.school.mapper.WrongQuestionMapper;
import com.smart.campus.school.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 定时任务调度(考勤统计任务)
 * 
 * @author 智慧校园项目组
 */
@Component("attendanceTask")
@Slf4j
public class AttendanceTask
{
    @Autowired
    private IAttendanceResultService attendanceResultService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {

        log.info("开始执行考勤统计任务");

        try {
            attendanceResultService.saveAttendanceResult(true);
        } catch (Exception e) {
           log.error("执行考勤统计任务异常",e);
        }finally {
            log.info("完成执行考勤统计任务");
        }

    }

    public void clearAttRecord(String date){
        log.info("开始执行考勤统计任务");
        try {
            attendanceResultService.clearAttendanceResult(date);
        } catch (Exception e) {
            log.error("执行考勤统计任务异常",e);
        }finally {
            log.info("完成执行考勤统计任务");
        }
    }

    public void clearAttRecord2(Integer type){
        log.info("开始执行考勤统计任务");
        try {
            List<String> datas = new ArrayList<>();
            switch (type){
                case 0:
                    datas.add(DateUtils.getDate());
                    break;
                case 1:
                    Date dNow = DateUtils.getNowDate();
                    Calendar c1 = Calendar.getInstance();
                    c1.setTime(dNow);
                    c1.add(Calendar.DAY_OF_MONTH,1);
                    datas.add(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,c1.getTime()));
                    break;
                case -1:
                    Date dNow1 = DateUtils.getNowDate();
                    Calendar c11 = Calendar.getInstance();
                    c11.setTime(dNow1);
                    c11.add(Calendar.DAY_OF_MONTH,-1);
                    datas.add(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,c11.getTime()));
                    break;
                default:
                    datas.add(DateUtils.getDate());
                    Date dNow2 = DateUtils.getNowDate();
                    Calendar c12 = Calendar.getInstance();
                    c12.setTime(dNow2);
                    for(int i=1;i<Math.abs(type);i++){
                        c12.add(Calendar.DAY_OF_MONTH,-1);
                        String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,c12.getTime());
                        log.info(date);
                        datas.add(date);
                    }

                    break;
            }
            for(String date:datas){
                attendanceResultService.clearAttendanceResult(date);
            }
        } catch (Exception e) {
            log.error("执行考勤统计任务异常",e);
        }finally {
            log.info("完成执行考勤统计任务");
        }
    }


}
